<template>
  <page-header-wrapper :title="false">
    <a-card title="保价记录">
      <template #extra>
        <a-tooltip>
          <template #title> 数据导出</template>
          <a-button type="primary" icon="database" @click="handleExpert(queryParam)"></a-button>
        </a-tooltip>
      </template>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="保价日期">
                <a-range-picker v-model="queryParam.date" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="筛选">
                <a-input placeholder="请输入" v-model="queryParam.keyword" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="loading" @click="handleSearch(queryParam)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" @click="handleAllDetails()">查看全部详情</a-button>
        <!-- <a-button icon="cloud-upload">导出</a-button> -->
      </div>

      <a-table ref="table" size="default" rowKey="id" :columns="columns" :data-source="loadData">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text, record">
          <template>
            <a-tag color="#82af6f" v-if="record?.checkStatus == 1">已审核</a-tag>
            <a-tag color="#abbac3" v-else>未审核</a-tag>
          </template>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#428bca" @click="handleDetail(record)">进入</a-tag>       
          </template>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getInsuredListUsingPOST , exportInsuredUsingPOST} from '@/api/api/caigouguanli.ts'
import moment from 'moment'
import { omit } from 'lodash-es'
export default {
  name: 'ChangePriceRecord',
  data() {
    return {
      mdl: null,
      visible: false,
      confirmLoading: false,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        pageNo:1,
        pageSize:10,
        queryCondition:'',
        date: [ moment().startOf('months').format('YYYY-MM-DD'), moment().format('YYYY-MM-DD') ],
        status: ''
      },
      pagination: {},
      loading: false,
      // 表头
      columns: [
        {
          title: '保价日期',
          dataIndex: 'orderDate',
        },
        {
          title: '保价供应商',
          dataIndex: 'supplyName',
        },
        {
          title: '摘要',
          dataIndex: 'description',
        },
        {
          title: '操作人',
          dataIndex: 'operator',
        },
        {
          title: '审核状态',
          dataIndex: 'checkStatus',
          scopedSlots: { customRender: 'status' },
        },
        {
          title: '备注',
          dataIndex: 'memo',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '220px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: [],
    }
  },
  created () {
    this.handleSearch(this.queryParam)
  },
  methods: {
    handleExpert(val){
      const params = {
        ...val,
        startTime: val?.date?.[0] ? moment(val?.date?.[0]).format('YYYY-MM-DD') : null,
        endTime: val?.date?.[1] ? moment(val?.date?.[1]).format('YYYY-MM-DD') : null,
      }
      exportInsuredUsingPOST({ ...omit(params, ['date'])}).then((res) => {
          const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;charset=utf-8',
          })
          const filename = '保价记录列表' + '.xlsx'
          // 将blob对象转为一个URL
          var blobURL = window.URL.createObjectURL(blob)
          // 创建一个a标签
          var tempLink = document.createElement('a')
          // 隐藏a标签
          tempLink.style.display = 'none'
          // 设置a标签的href属性为blob对象转化的URL
          tempLink.href = blobURL
          // 给a标签添加下载属性
          tempLink.setAttribute('download', filename)
          if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank')
          }
          // 将a标签添加到body当中
          document.body.appendChild(tempLink)
          // 启动下载
          tempLink.click()
          // 下载完毕删除a标签
          document.body.removeChild(tempLink)
          window.URL.revokeObjectURL(blobURL)
          this.$message.success('导出成功~')
        })  
    },
    handleSearch(val) {
      this.loading = true
      const params = {
        ...val,
        startTime: val?.date?.[0] ? moment(val?.date?.[0]).format('YYYY-MM-DD') : null,
        endTime: val?.date?.[1] ? moment(val?.date?.[1]).format('YYYY-MM-DD') : null,
      }
      getInsuredListUsingPOST({ ...omit(params, ['date']) }).then((res) => {
        this.loadData = res.data.list
          this.queryParam.pageNo = this.pagination.current
        this.pagination = { ...this.pagination, total: Number(res.data.total) }
        this.loading = false
      })
      .catch((error) => {
        this.loading = false
        this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
      })
    },
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
 pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.handleSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo:pagination.current,
        pageSize:pagination.pageSize,
        queryCondition:this.queryParam.queryCondition,
        status:this.queryParam.status,
        date:this.queryParam.date
      })
    },
    handleAllDetails() {
      this.$router.push({ path: `/purchasing/ProtectPriceRecord/alldetails`, query: {
        startTime: this.queryParam?.date?.[0] ? moment(this.queryParam?.date?.[0]).format('YYYY-MM-DD') : null,
        endTime: this.queryParam?.date?.[1] ? moment(this.queryParam?.date?.[1]).format('YYYY-MM-DD') : null,
      } })
    },
    handleDetail(record) {
      this.$router.push({ path: `/purchasing/ProtectPriceRecord/${record.id}` })
    }
  },
}
</script>
